home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / screen-profiles / battery < prev    next >
Encoding:
Text File  |  2009-04-08  |  2.4 KB  |  98 lines

  1. #!/bin/sh -e
  2. #
  3. #  battery: print the state of the battery
  4. #  Copyright (C) 2009 Rapha√´l Pinson.
  5. #
  6. #  Authors: Rapha√´l Pinson <raphink@ubuntu.com>
  7. #           Dustin Kirkland <kirkland@canonical.com>
  8. #
  9. #  This program is free software: you can redistribute it and/or modify
  10. #  it under the terms of the GNU General Public License as published by
  11. #  the Free Software Foundation, version 3 of the License.
  12. #
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21.  
  22. search () {
  23.    local str expr
  24.    str="$1"
  25.    expr="$2"
  26.  
  27.    echo "$str" | sed -n "s/${expr}/\1/p"
  28. }
  29.  
  30.  
  31. BATS=$(ls /proc/acpi/battery)
  32. NB=$(echo "$BATS" | wc -l)
  33.  
  34. for bat in $BATS; do
  35.    if [ "$NB" -gt 1 ]; then
  36.       echo -n "$bat: "
  37.    fi
  38.  
  39.    # read files once
  40.    infofile=$(cat "/proc/acpi/battery/$bat/info")
  41.    statefile=$(cat "/proc/acpi/battery/$bat/state")
  42.  
  43.    present=$(search "$infofile" "present: *\(.*\)")
  44.  
  45.    if [ "x${present}" = "xno" ]; then
  46.       echo "n/a"
  47.       break
  48.    fi
  49.  
  50.    full=$(search "$infofile" "last full capacity: *\(.*\) m[AW]h")
  51.    warn=$(search "$infofile" "design capacity warning: \(.*\) m[AW]h")
  52.    low=$(search "$infofile" "design capacity low: \(.*\) m[AW]h")
  53.  
  54.    rem=$(search "$statefile" "remaining capacity: *\(.*\) m[AW]h")
  55.    if [ "$rem" -lt "$low" ]; then
  56.       cap_color="{= rk}"
  57.    elif [ "$rem" -lt "$warn" ]; then
  58.       cap_color="{= yk}"
  59.    else
  60.       cap_color="{= gk}"
  61.    fi
  62.  
  63.  
  64.    percent=$( echo "$rem" "$full" | awk '{printf "%.0f",  100*$1/$2}')
  65.    if [ "$percent" -lt 17 ]; then
  66.     per_color="{= rk}"
  67.    elif [ "$percent" -lt 33 ]; then
  68.     per_color="{= Rk}"
  69.    elif [ "$percent" -lt 50 ]; then
  70.     per_color="{= yk}"
  71.    elif [ "$percent" -lt 67 ]; then
  72.     per_color="{= Yk}"
  73.    elif [ "$percent" -lt 83 ]; then
  74.     per_color="{= Gk}"
  75.    else
  76.     per_color="{= gk}"
  77.    fi
  78.  
  79.  
  80.    state=$(search "$statefile" "charging state: *\(.*\)")
  81.    case $state in
  82.       charging)
  83.          sign="+"
  84.          ;;
  85.       discharging)
  86.          sign="-"
  87.          ;;
  88.       charged)
  89.          sign="="
  90.          ;;
  91.       *)
  92.          sign="$state"
  93.          ;;
  94.    esac
  95.  
  96.    printf "\005%s%d%%\005{-}\005%s|%s|\005{-} " "$per_color" "$percent" "$cap_color" "$sign"
  97. done
  98.